# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from lxml import etree
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
import time
import requests


"""
知网-报纸
http://dbpub.cnki.net/Grid2008/Dbpub/Brief.aspx?ID=SCPD&subBase=fm
"""



# 初始化
def init_driver(url):
    chrome_options = Options()
    # chrome_options.add_argument('--headless') # 无头模式
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_experimental_option('excludeSwitches', ['enable-automation']) # 设置开发模式，避免被检测
    # driver = webdriver.Chrome(chrome_options=chrome_options) # 已经设置PATH环境变量
    # 未在PATH环境变量中设置，需要设置驱动可执行路径
    driver = webdriver.Chrome(executable_path='D:\\webdriver\\chromedriver.exe', chrome_options=chrome_options)
    driver.maximize_window()  # 窗口最大化
    driver.get(url)
    # html = driver.page_source
    # print(html)
    return driver


# 获取页面url
def get_url(drive):
    # 设置等待
    wait = WebDriverWait(driver, 10)

    time.sleep(5)

    s3 = Select(driver.find_element_by_id('magazine_special1'))  # 实例化Select
    s3.select_by_visible_text("精确")
    driver.find_element_by_xpath('//td[@class="aomBtn"]/a').click()
    driver.find_element_by_id('txt_1_value1').send_keys("美的集团")
    driver.find_element_by_id('txt_2_value1').send_keys("美的集团")
    driver.find_element_by_id('txt_3_value1').send_keys("美的集团")
    driver.find_element_by_id('magazine_value1').send_keys("中国证券报")
    driver.find_element_by_id('publishdate_from').send_keys("2008-01-01")
    driver.find_element_by_id('publishdate_to').send_keys("2019-12-31")
    driver.find_element_by_id('btnSearch').click()
    time.sleep(3)
    driver.switch_to.frame("iframeResult")
    a = True
    while a:
        try:
            WebDriverWait(driver, 25).until(
                EC.presence_of_element_located((By.CLASS_NAME, "GridTableContent")))
            break
        except:
            pass


    while True:
        selector = etree.HTML(driver.page_source)
        xpath = selector.xpath('//table[@class="s_table"]/tbody/tr')
        i = 0
        for x in xpath:
            if i > 0:
                name = x.xpath('./td[@class="s_tabletd_rb"]/a')[1].text
                url = "http://dbpub.cnki.net/Grid2008/Dbpub/" + x.xpath('./td[@class="s_tabletd_rb"]/a/@href')[0]
                fmr = x.xpath('./td[@class="s_tabletd_rb"]')[2].text.replace("\n", "")
                sqr = x.xpath('./td[@class="s_tabletd_rb"]')[3].text.replace("\n", "")
                rq = x.xpath('./td[@class="s_tabletd_rb"]')[4].text.replace("\n", "")
                gkrq = x.xpath('./td[@class="s_tabletd_rb"]')[5].text.replace("\n", "")
                print("=======================")
                print(name)
                print(url)
                print(sqr)
                print(rq)
                print(gkrq)
                print("=======================")
                # js=f'window.open("{url}");'
                # driver.execute_script(js)
                headers = {
                    "Accept": "text/html, */*; q=0.01",
                    "Accept-Encoding": "gzip, deflate",
                    "Accept-Language": "zh-CN,zh;q=0.9",
                    "Connection": "keep-alive",
                    "Cookie": "JSESSIONID=BF6B6B051F703A774FB2681E1BA9A73E",
                    "DNT": "1",
                    "Host": "www.ccgp-anhui.gov.cn",
                    "Referer": "http://www.ccgp-anhui.gov.cn/",
                    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36",
                    "X-Requested-With": "XMLHttpRequest"
                }
                html = requests.get(url)
                selector1 = etree.HTML(html.text)
                sqh = selector1.xpath('//table[@id="box"]/tr[1]/td')[1].text  # 申请号
                sqr = selector1.xpath('//table[@id="box"]/tr[1]/td')[3].text  # 申请日
                gkh = selector1.xpath('//table[@id="box"]/tr[2]/td')[1].text  # 公开号
                gkr = selector1.xpath('//table[@id="box"]/tr[2]/td')[3].text  # 公开日
                sqr = selector1.xpath('//table[@id="box"]/tr[3]/td')[1].text  # 申请人
                dz = selector1.xpath('//table[@id="box"]/tr[3]/td')[3].text  # 地址
                gtsqr = selector1.xpath('//table[@id="box"]/tr[4]/td')[1].text  # 共同申请人
                fmr = selector1.xpath('//table[@id="box"]/tr[5]/td')[1].text  # 发明人
                dljg = selector1.xpath('//table[@id="box"]/tr[8]/td')[1].text  # 专利代理机构
                dlr = selector1.xpath('//table[@id="box"]/tr[8]/td')[3].text  # 代理人
                gsdm = selector1.xpath('//table[@id="box"]/tr[10]/td')[1].text  # 国省代码
                des = selector1.xpath('//table[@id="box"]/tr[11]/td')[1].text  # 摘要
                zqx = selector1.xpath('//table[@id="box"]/tr[12]/td')[1].text  # 主权项
                ys = selector1.xpath('//table[@id="box"]/tr[13]/td')[1].text  # 页数
                zflh = selector1.xpath('//table[@id="box"]/tr[14]/td')[1].text  # 主分类号
                zlfh1 = selector1.xpath('//table[@id="box"]/tr[15]/td')[1].text  # 专利分类号
                print(dljg)
                print(dlr)
                print(zflh)
            i += 1
        # 点击下一页
        driver.find_element_by_xpath('//div[@class="TitleLeftCell"]//a[contains(text(),"下一页")]').click()
        # 点击下一页
    pass
        # time.sleep(5)






def close_driver(driver):
    driver.quit()

if __name__ == '__main__':
    url = "https://kns.cnki.net/kns/brief/result.aspx?dbprefix=CCND"
    driver = init_driver(url)
    get_url(driver)
